package cn.itcast.web.controller.company;

import cn.itcast.domain.company.Company;
import cn.itcast.service.CompanyService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("company")
public class CompanyController {

    @Reference
    private CompanyService companyService;
    @RequestMapping(value = "list" , method = RequestMethod.GET)
    public String  findAll(ModelMap modelMap){
        List<Company> list = companyService.findAll();
        modelMap.addAttribute("list",list);
        return "company/company-list";
    }


    /**
     * 添加
     * 功能入口：company-list
     * 请求地址：company/toAdd
     * 请求参数：Company company
     * 响应地址：company-add
     */
    @RequestMapping("toAdd")
    public String add(){
        return "company/company-add";
    }


    /**
     * 添加and修改
     * 功能入口:company-add
     * 请求地址:company/edit
     * 请求参数:Company company
     * 响应地址:company-list
     */

    @RequestMapping("edit")
    public String save(Company company){

       if (StringUtils.isEmpty(company.getId())){
           //判断：如果Id为空，则为添加操作；
           System.out.println(company);
           companyService.save(company);
       }else {
           //判断：如果Id不为空，则为修改操作
          companyService.update(company);

       }

        return "redirect:list";
    }

    /**
     * 修改的数据回显
     * 功能入口:company-add
     * 请求地址:company/toUpdate
     * 请求参数:Company company
     * 响应地址:company-list
     */
   @RequestMapping("/toUpdate")
       public String toUpdate(String id , Model model){
          Company company=companyService.findId(id);
       System.out.println(id);
       model.addAttribute("company",company);
       return "company/company-update";
   }

    /**
     * 删除方法
     * 功能入口:company-list
     * 请求地址:/company/delete
     * 请求参数:String id
     * 响应地址:company-list
     */
@RequestMapping("delete")
public String delete(String id){

    companyService.delete(id);
    return "redirect:list";
}



}
